package com.yic.module.wms.controller.admin.warehousearea;

import cn.hutool.core.util.ObjectUtil;
import com.yic.framework.common.enums.CommonStatusEnum;
import com.yic.module.base.api.factory.FactoryApi;
import com.yic.module.wms.controller.admin.warehouse.vo.WarehouseSimpleRespVO;
import com.yic.module.wms.convert.warehouse.WarehouseConvert;
import com.yic.module.wms.dal.dataobject.warehouse.WarehouseDO;
import com.yic.module.wms.service.warehouse.WarehouseService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.yic.framework.common.pojo.PageResult;
import com.yic.framework.common.pojo.CommonResult;
import static com.yic.framework.common.pojo.CommonResult.success;

import com.yic.framework.excel.core.util.ExcelUtils;

import com.yic.framework.operatelog.core.annotations.OperateLog;
import static com.yic.framework.operatelog.core.enums.OperateTypeEnum.*;

import com.yic.module.wms.controller.admin.warehousearea.vo.*;
import com.yic.module.wms.dal.dataobject.warehousearea.WarehouseAreaDO;
import com.yic.module.wms.convert.warehousearea.WarehouseAreaConvert;
import com.yic.module.wms.service.warehousearea.WarehouseAreaService;

@Tag(name = "管理后台 - 库区")
@RestController
@RequestMapping("/wms/warehouse-area")
@Validated
public class WarehouseAreaController {

    @Resource
    private WarehouseAreaService warehouseAreaService;
    @Resource
    private WarehouseService warehouseService;

    @PostMapping("/create")
    @Operation(summary = "创建库区")
    @PreAuthorize("@ss.hasPermission('wms:warehouse-area:create')")
    public CommonResult<Long> createWarehouseArea(@Valid @RequestBody WarehouseAreaCreateReqVO createReqVO) {
        return success(warehouseAreaService.createWarehouseArea(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新库区")
    @PreAuthorize("@ss.hasPermission('wms:warehouse-area:update')")
    public CommonResult<Boolean> updateWarehouseArea(@Valid @RequestBody WarehouseAreaUpdateReqVO updateReqVO) {
        warehouseAreaService.updateWarehouseArea(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除库区")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('wms:warehouse-area:delete')")
    public CommonResult<Boolean> deleteWarehouseArea(@RequestParam("id") Long id) {
        warehouseAreaService.deleteWarehouseArea(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得库区")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('wms:warehouse-area:query')")
    public CommonResult<WarehouseAreaRespVO> getWarehouseArea(@RequestParam("id") Long id) {
        WarehouseAreaDO warehouseArea = warehouseAreaService.getWarehouseArea(id);
        return success(WarehouseAreaConvert.INSTANCE.convert(warehouseArea));
    }

    @GetMapping("/list")
    @Operation(summary = "获得库区列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('wms:warehouse-area:query')")
    public CommonResult<List<WarehouseAreaRespVO>> getWarehouseAreaList(@RequestParam("ids") Collection<Long> ids) {
        List<WarehouseAreaDO> list = warehouseAreaService.getWarehouseAreaList(ids);
        return success(WarehouseAreaConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得库区分页")
    @PreAuthorize("@ss.hasPermission('wms:warehouse-area:query')")
    public CommonResult<PageResult<WarehouseAreaRespVO>> getWarehouseAreaPage(@Valid WarehouseAreaPageReqVO pageVO) {
        PageResult<WarehouseAreaDO> pageResult = warehouseAreaService.getWarehouseAreaPage(pageVO);
        PageResult<WarehouseAreaRespVO> respVo = WarehouseAreaConvert.INSTANCE.convertPage(pageResult);
        respVo.getList().forEach(resp -> {
            //封装仓库
            WarehouseDO warehouse = warehouseService.getWarehouse(resp.getWarehouseId());
            if (ObjectUtil.isNotEmpty(warehouse)) {
                resp.setWarehouseName(warehouse.getName());
            }
        });
        return success(respVo);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出库区 Excel")
    @PreAuthorize("@ss.hasPermission('wms:warehouse-area:export')")
    @OperateLog(type = EXPORT)
    public void exportWarehouseAreaExcel(@Valid WarehouseAreaExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<WarehouseAreaDO> list = warehouseAreaService.getWarehouseAreaList(exportReqVO);
        // 导出 Excel
        List<WarehouseAreaExcelVO> datas = WarehouseAreaConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "库区.xls", "数据", WarehouseAreaExcelVO.class, datas);
    }

    @GetMapping("/list-all-simple")
    @Operation(summary = "获取库区精简信息列表", description = "只包含被开启的库区，主要用于前端的下拉选项")
    public CommonResult<List<WarehouseAreaSimpleRespVO>> getSimpleWarehouseAreaList(@RequestParam("warehouseId") Long warehouseId) {
        // 获得库区列表，只要开启状态的
        List<WarehouseAreaDO> list = warehouseAreaService.getWarehouseAreaList(null, warehouseId, Collections.singleton(CommonStatusEnum.ENABLE.getStatus()));
        // 排序后，返回给前端
        list.sort(Comparator.comparing(WarehouseAreaDO::getSort));
        List<WarehouseAreaSimpleRespVO> warehouseAreaSimpleRespVOS = WarehouseAreaConvert.INSTANCE.convertList03(list);
        warehouseAreaSimpleRespVOS.forEach(warehouseAreaSimpleRespVO -> {
            warehouseAreaSimpleRespVO.setIndex("1-" + warehouseAreaSimpleRespVO.getId());
        });
        return success(warehouseAreaSimpleRespVOS);
    }

}
